/*     */ package com.hp.bon.sgw.core.channel;
/*     */ 
/*     */ import com.hp.bon.sgw.bean.XmlMessage;
/*     */ import com.hp.bon.sgw.core.CallParam;
/*     */ import com.hp.bon.sgw.core.channel.handler.NPHandler;
/*     */ import com.hp.bon.sgw.domain.Node;
/*     */ import com.hp.bon.sgw.domain.SGWChannel;
/*     */ import com.hp.bon.sgw.service.ConfigService;
/*     */ import com.hp.bon.sgw.util.Comutils;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.net.InetAddress;
/*     */ import java.net.InetSocketAddress;
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ import javax.xml.stream.XMLInputFactory;
/*     */ import javax.xml.stream.XMLStreamReader;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.apache.commons.httpclient.methods.RequestEntity;
/*     */ import org.apache.commons.httpclient.methods.StringRequestEntity;
/*     */ import org.simpleframework.http.Request;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ public class NPChannel extends SoapChannel
/*     */ {
/*  26 */   private static final Logger logger = LoggerFactory.getLogger(NPChannel.class);
/*     */ 
/*  33 */   private static String NP_host = "NP@001.ctnbc-bon.net";
/*  34 */   private static String HSS_host = "HSS_NP@001.ctnbc-bon.net";
/*     */ 
/*     */   public NPChannel(SGWChannel channel)
/*     */   {
/*  29 */     super(channel);
/*     */   }
/*     */ 
/*     */   protected void dealWithOtherReqPath(long reqTime, Request request, PrintStream body, String reqPath)
/*     */     throws Exception
/*     */   {
/*  37 */     if (reqPath.endsWith("/ws/LSMSService")) {
/*  38 */       XmlMessage message = new XmlMessage(true);
/*  39 */       message.setLength(request.getContent().getBytes().length);
/*  40 */       message.setMID(getElementValue("MessageID", request.getContent()));
/*  41 */       message.setIp(request.getClientAddress() != null ? request.getClientAddress().getAddress().getHostAddress() : "");
/*  42 */       message.setMsgTime(reqTime);
/*  43 */       message.setDATA(request.getContent());
/*     */ 
/*  53 */       String commandCode = getElementValue("CommandCode", request.getContent());
/*  54 */       NPHandler asynHandler = new NPHandler(body);
/*  55 */       CallParam callParam = null;
/*  56 */       if (commandCode.endsWith("IND")) {
/*  57 */         callParam = new CallParam(message.getMID(), "IND.NP@001.ctnbc-bon.net", message, NP_host, asynHandler);
/*  58 */         callParam.setFromNode((Node)ConfigService.name2NodeMap.get(NP_host));
/*  59 */         callParam.setToNode((Node)ConfigService.name2NodeMap.get(HSS_host));
/*     */       } else {
/*  61 */         callParam = new CallParam(message.getMID(), "CONFIRM.NP@001.ctnbc-bon.net", message, HSS_host, asynHandler);
/*  62 */         callParam.setFromNode((Node)ConfigService.name2NodeMap.get(HSS_host));
/*  63 */         callParam.setToNode((Node)ConfigService.name2NodeMap.get(NP_host));
/*     */       }
/*  65 */       if (callParam.getToNode() == null) {
/*  66 */         logger.warn(message.getMID() + ", cannot find target node.");
/*  67 */         return;
/*     */       }
/*  69 */       String response = forwardRequest(callParam.getToNode().getServerUrl(), ((XmlMessage)callParam.getRequest()).getDATA());
/*     */ 
/*  71 */       if (response != null) {
/*  72 */         XmlMessage respMsg = new XmlMessage(false);
/*  73 */         respMsg.setMsgTime(System.currentTimeMillis());
/*     */ 
/*  75 */         String responseCode = getElementValue("ResponseCode", response);
/*  76 */         if ((responseCode != null) && (!"".equals(responseCode))) {
/*  77 */           callParam.setRetCode(Integer.valueOf(responseCode).intValue());
/*  78 */           respMsg.setRC(responseCode);
/*     */         }
/*  80 */         respMsg.setLength(response.getBytes().length);
/*  81 */         respMsg.setDATA(response);
/*  82 */         callParam.setRecvResponse(respMsg);
/*  83 */         callParam.setRetCode(0);
/*     */       }
/*  85 */       asynHandler.onCallResult(callParam);
/*     */     }
/*     */     else {
/*  88 */       body.println("No service provided at this address path ");
/*  89 */       body.close();
/*     */     }
/*     */   }
/*     */ 
/*     */   public int sendRequest(CallParam callParam)
/*     */   {
/* 118 */     return 0;
/*     */   }
/*     */ 
/*     */   public static String getElementValue(String element, String content) {
/*     */     try {
/* 123 */       InputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
/* 124 */       XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(input);
/* 125 */       int nextType = 0;
/* 126 */       while (xmlStreamReader.hasNext()) {
/* 127 */         nextType = xmlStreamReader.next();
/* 128 */         if (nextType == 1) {
/* 129 */           String elName = xmlStreamReader.getLocalName();
/* 130 */           if (element.equals(elName))
/* 131 */             return xmlStreamReader.getElementText();
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (Exception e) {
/* 136 */       e.printStackTrace();
/*     */     }
/* 138 */     return null;
/*     */   }
/*     */ 
/*     */   private String forwardRequest(String url, String content) {
/* 142 */     PostMethod post = new PostMethod(url);
/*     */     try {
/* 144 */       RequestEntity entity = new StringRequestEntity(content, "text/xml", "UTF-8");
/* 145 */       post.setRequestEntity(entity);
/* 146 */       HttpClient httpclient = new HttpClient();
/* 147 */       int result = httpclient.executeMethod(post);
/* 148 */       logger.info("Http post result code :" + result);
/* 149 */       if (result == 200)
/*     */       {
/* 154 */         InputStream resp = post.getResponseBodyAsStream();
/* 155 */         return Comutils.InputStreamTOString(resp, "UTF-8");
/*     */       }
/*     */ 
/* 158 */       post.abort();
/* 159 */       logger.warn("Http post NP service error,result:" + result);
/* 160 */       return null;
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 164 */       logger.error("NP service has error :" + e.toString());
/*     */     } finally {
/* 166 */       post.releaseConnection();
/*     */     }
/* 168 */     return null;
/*     */   }
/*     */ }

/* Location:           C:\Users\think\Desktop\sgw_2.1.jar
 * Qualified Name:     com.hp.bon.sgw.core.channel.NPChannel
 * JD-Core Version:    0.6.1
 */